package com.jftone.bh.app.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.jftone.config.PropertyConfigurer;
import org.jftone.util.DataMap;
import org.jftone.util.FileUtil;
import org.jftone.util.IData;

public class PayParamUtil {
	public static final String RESULT_SUCC = "SUCCESS";		//支付成功返回值
	public static final String RESULT_FAIL = "FAIL";		//支付失败返回值
	/**
	 * 除去数组中的空值和签名参数
	 * @param inData 
	 * @param filerData
	 * @return  去掉空值与签名参数后的新签名参数组
	 */
    public static IData<String, Object> getFilterMap(IData<String, Object> params, IData<String, Object> filerData) {
    	IData<String, Object> result = new DataMap<String, Object>();
        if (params == null || params.size() <= 0) {
            return result;
        }
        for (String key : params.keySet()) {
            String value = params.getString(key, "");
            if (value == null || value.equals("") || filerData.containsKey(key)) {
                continue;
            }
            result.put(key, value);
        }
        return result;
    }

    /** 
     * 把数组所有元素排序，并按照“参数=参数值”的模式用“&”字符拼接成字符串
     * @param params 需要排序并参与字符拼接的参数组
     * @return 拼接后字符串
     */
    public static String createLinkString(IData<String, Object> params) {
        List<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);
        StringBuilder prestr = new StringBuilder();
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = String.valueOf(params.get(key));
            //拼接时，不包括最后一个&字符
            prestr.append((i>0? "&" : "")+key + "=" + value);
        }
        return prestr.toString();
    }
    
    public static String getOrderSubject(String tradeSeq) {
        return "MoGo订单-"+tradeSeq;
    }
    
    
    public static void writePayLog(IData<String,  Object> data){
    	if(data == null || data.isEmpty()) return;
		StringBuilder logs = new StringBuilder();
		int i = 0;
		for(Map.Entry<String, Object> entry : data.entrySet()){
			logs.append((i == 0? "" : ",") +entry.getKey()+":"+entry.getValue().toString());
			i++;
		}
		try {
			FileUtil.writeFile(PropertyConfigurer.get("wx.paylog"), logs.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
